package com.msb.dp.strategy;

/**
 * @Auther: Huanyu Lee
 * @Date: 2022/3/3  17:21
 * @Description: 选择排序
 */
public class Sorter<T>{
    public void sort(T[] arr,Comparator<T> cmp){
        for (int i = 0; i < arr.length; i++) {
           int minpos = i;
            for (int j = i+1; j < arr.length; j++) {
                minpos = cmp.compare(arr[j],arr[minpos]) == -1 ? j : minpos;
            }
            swap(arr,i,minpos);
        }
    }

    private void swap(T[] arr, int i, int j) {
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
